﻿
//===============================================================================
//	GameServer.h
//	游戏服务器
//===============================================================================

#pragma once

#ifdef _MSC_VER
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#endif


#include "../share/common_singleton.h"
#include "../share/server_base.h"
#include "../share/server_net_new.h"
#include "../share/code_def.h"
#include "game_msg.h"
#include "../share/proxy_msg_mgr.h"
#include "../share/channel_base.h"
#include "../share/net_msg_transfer.h"
#include "../share/net_event_manager.h"

class CGameChannel :
	public ::common::net::CChannelBase,
	public ::common::CSingleton<CGameChannel>
{
public:
	virtual void OnMsgEvent();

	virtual void OnConnect();

	virtual void OnDisconnect();
};

class GameServer :
	public IService,
	public ::common::CSingleton<GameServer>
{
public:
	GameServer();
	~GameServer();

public:
	int32_t Initialize(void);
	virtual int32_t ServerStart();
	int32_t Finalize();

	virtual void TickUpdate();

	void OnNewDay();

	void OnNewZeroDay();

	void OnNewWeek();

	virtual int32_t ServiceMainLoop();
	void SceneServiceWattingExit();
	virtual void OnUserSignal1();

	int32_t client_msg_dispatch(std::string& msg, unsigned long uid, uint32_t msgId);
	bool send_by_uid(uint32_t uid, std::string& msg, uint32_t msgId);
	void broadcast_msg_to_client(std::string& msg, uint32_t msgId);
	virtual void client_close_socket(uint32_t uid);
	virtual void del_client_socket(uint32_t fd);
	bool is_connnected(uint32_t uid);
	virtual EServiceType GetServiceType() { return GAME_SERVICE; }

public:
	virtual int32_t AddClientMsg(uint32_t socketId, uint32_t msgId, std::string &msg);
	virtual void ProcessClientMsg(CClientSocketMsg& val);
public:
	void RegisteClientMsg(uint32_t msg_id, GamePlayerMsgProcess fun)
	{
		game_client_msg_mgr_.RegisteMsgJson(msg_id, fun);
	}
	void RegisteServerMsg(uint32_t msg_id, ProxyMsgProcess func)
	{
		proxy_msg_mgr_.RegisteMsgJson(msg_id, func);
	}
	CGamePlayerMsgFactory& GetPlayerMsgFactory() { return game_client_msg_mgr_; }

	void SetGateFd(uint32_t gate_fd) { client_gate_fd = gate_fd; }

public:
	int32_t SendToGate(std::string& msg, uint32_t msgid);
private:
	//CServerNet server_net;
	::common::net::CServerNet<CGameChannel> game_net_;

	CGamePlayerMsgFactory game_client_msg_mgr_;

	time_t update_time_;

	int32_t client_gate_fd;
};

#define registe_client_msg GameServer::GetInstance().RegisteClientMsg
#define registe_server_msg GameServer::GetInstance().RegisteServerMsg

#define send_gate_2_gate  GameServer::GetInstance().SendToGate